<template>
  <div>
    <el-button type="primary"
               v-hasPermi="['car:customer:add']"
               @click="openAddDialog(null)">添加客户</el-button>

    <el-table v-loading="loading" :data="tableData">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="用户头像" align="center" prop="customerAvatar" v-if="columns[1].visible" width="160">
        <template v-slot="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.customerAvatar"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" align="center" key="id" prop="id" v-if="columns[0].visible" :show-overflow-tooltip="true" />
      <el-table-column label="姓名" align="center" key="customerName" prop="customerName" v-if="columns[2].visible" :show-overflow-tooltip="true" />
      <el-table-column label="出生日期" align="center" key="birthDate" prop="birthDate" v-if="columns[3].visible" :show-overflow-tooltip="true" />
      <el-table-column label="性别" align="center" key="gender" prop="gender" v-if="columns[4].visible" width="120" />
      <el-table-column label="地址" width="120">
        <template v-slot="scope">
          {{scope.row.provinceName}}/{{scope.row.cityName}}/{{scope.row.districtName}}
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" key="email" prop="email" v-if="columns[8].visible" width="120" />
      <el-table-column label="创建时间" align="center" key="createTime" prop="createTime" v-if="columns[9].visible" width="120" />
      <el-table-column label="创建人" align="center" key="createdBy" prop="createdBy" v-if="columns[10].visible" width="120" />
      <el-table-column label="更新时间" align="center" key="updateTime" prop="updateTime" v-if="columns[11].visible" width="120" />
      <el-table-column label="状态" align="center" key="customerStatus" v-if="columns[12].visible">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.customerStatus"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleLock(scope.row)">
            <span>{{scope.row.customerStatus === 0? '锁定账号' : '解锁账号'}}</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑客户的 Dialog -->
    <el-dialog :title="title" :visible.sync="addDialogVisible">
      <el-form ref="addForm" :model="addFormData">
        <el-form-item label="头像" prop="customerAvatar">
          <el-upload
            class="avatar-uploader"
            action="http://localhost/dev-api/car/customer/customerUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="姓名" prop="customerName">
          <el-input v-model="addFormData.customerName"></el-input>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker v-model="addFormData.birthDate" type="date"></el-date-picker>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group vngModel="addFormData.gender">
            <el-radio label="Male">男</el-radio>
            <el-radio label="Female">女</el-radio>
            <el-radio label="Other">其他</el-radio>
          </el-radio-group>
        </el-form-item>

       地址: <el-cascader
          v-model="value"
          :options="options"
          :props="{expandTrigger: 'hover',value: 'id',label: 'name'}"
          @change="handleChange">
        </el-cascader>

        <el-form-item label="登录密码" prop="loginPassword">
          <el-input v-model="addFormData.loginPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addFormData.email"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addFormData.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitData()">{{ buttonText }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addCustomer, fetchData, getAreaTree, lockAccount, unitAccount} from "@/views/car/customer/customer";

export default {
  name: "index",
  data() {
    return {
      loading: true,
      tableData: [],
      title: '',
      addDialogVisible: false,
      addFormData: {
        customerAvatar: '',
        customerName: '',
        birthDate: null,
        gender: '',
        provinceId: '',
        cityId: '',
        districtId: '',
        loginPassword: '',
        email: '',
        remark: ''
      },
      isEdit: false,
      imageUrl: '',
      buttonText: '确定',
      value: [],
      options: [{
        value: '',
        label: '',
        children: []
      }],
      props:{
        value: 'id',
        label: 'name'
      },
      columns: [
        { key: 0, label: `用户ID`, visible: true },
        { key: 1, label: `用户头像`, visible: true },
        { key: 2, label: `姓名`, visible: true },
        { key: 3, label: `出生日期`, visible: true },
        { key: 4, label: `性别`, visible: true },
        { key: 5, label: `省份`, visible: true },
        { key: 6, label: `市`, visible: true },
        { key: 7, label: `区`, visible: true },
        { key: 8, label: `邮箱`, visible: true },
        { key: 9, label: `创建时间`, visible: true },
        { key: 10, label: `创建人`, visible: true },
        { key: 11, label: `更新时间`, visible: true },
        { key: 12, label: `状态`, visible: true }
      ],

    }
  },
  methods: {
    handleStatusChange() {

    },
    handleChange(value) {
      console.log(value);
    },
    handleLock(row) {
      let id = row.id;
      if (row.customerStatus === 0) {
        lockAccount(id).then(res => {
          if (res.code == 200) {
            this.$message.success(res.data);
            this.fetchData()
          }
        });
      }else {
        unitAccount(id).then(res => {
          if (res.code == 200) {
            this.$message.success(res.data)
            this.fetchData();
          }
        });
      }
    },
    getAreaTree() {
      getAreaTree().then(res => {
        this.options = res.data
        console.log(res.data)
      });
    },
    updataCustomer(row) {
      this.axios.post("/customer/CustomerUpdata", row).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.msg);
        } else {
          this.$message.error(res.data.msg);
        }
        this.fetchData();
      });
    },
    handleClick(row) {
      console.log("查看操作", row);
      // 可以在这里添加查看详情的逻辑
    },
    /** 查询客户列表 */
    fetchData() {
      this.loading = true;
      fetchData().then(response => {
        console.log(response.data)
          this.tableData = response.data;
          // this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    openAddDialog(row) {
      this.getAreaTree();
      if (row) {
        this.isEdit = true;
        this.title = "编辑信息";
        this.buttonText = '保存修改';
        this.addFormData = {...row };
      } else {
        this.isEdit = false;
        this.title = "添加客户";
        this.buttonText = '确定';
        this.addFormData = {
          customerAvatar: '',
          customerName: '',
          birthDate: null,
          gender: '',
          provinceId: '',
          cityId: '',
          districtId: '',
          loginPassword: '',
          email: '',
          remark: ''
        };
      }
      this.addDialogVisible = true;
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.addFormData.customerAvatar = res.data
      console.log(res.data)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    submitData() {
      if (this.isEdit) {
        this.updataCustomer(this.addFormData);
      } else {
        console.log("aaaaaaa")
        this.addCustomer();
      }
      this.addDialogVisible = false;
    },
    addCustomer() {
      // 发送添加客户的请求到后端
      addCustomer(this.addFormData).then(res=>{
        if (res.data.code == 200) {
          this.$message.success(res.data.msg);
          this.fetchData();
        } else {
          this.$message.error(res.data.msg)
        }

      });
    }
  },
  created() {
    this.fetchData();
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
